package com.lg.partition14;

import java.io.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

/**
 * @author `RKC`
 * @date 2022/4/20 17:11
 */
public class P1346电车 {

    private static final int N = 110, M = N * N, INF = 0x3f3f3f3f;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static int[] dist = new int[N];
    private static boolean[] st = new boolean[N];
    private static Deque<Integer> deque = new ArrayDeque<>(N);
    private static int n, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        int s = nextInt(), t = nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) {
            int k = nextInt();
            for (int j = 0; j < k; j++) {
                int v = nextInt();
                if (j == 0) add(i, v, 0);
                else add(i, v, 1);
            }
        }
        writer.println(bfs(s, t));
        writer.flush();
    }

    private static int bfs(int s, int t) {
        Arrays.fill(dist, INF);
        deque.addLast(s);
        dist[s] = 0;
        while (!deque.isEmpty()) {
            int u = deque.pollFirst();
            if (u == t) return dist[u];
            if (st[u]) continue;
            st[u] = true;
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (dist[v] > dist[u] + w[i]) {
                    if (w[i] == 0) deque.addFirst(v);
                    else deque.addLast(v);
                    dist[v] = dist[u] + w[i];
                }
            }
        }
        return -1;
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
